package com.lqs.sofp.utils;

import com.lqs.sofp.dao.UserInfoDao;
import com.lqs.sofp.dao.impl.UserInfoDaoImpl;
import com.lqs.sofp.entity.User;
import com.lqs.sofp.service.UserService;
import com.lqs.sofp.service.impl.UserServiceImpl;

import java.sql.*;

public class DbUtils {
    static String URL = "jdbc:mysql://47.113.195.222:3306/JavaWebPrjDB?serverTimezone=Asia/Chongqing&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true";
    //static String URL = "jdbc:mysql://localhost:3306/javawebprjdb?serverTimezone=Asia/Chongqing&useUnicode=true&characterEncoding=utf8";
    static String userName = "root";
    static String password = "123456";

    /*启动驱动*/
    public static void registerDriver() {
        try {
            Class.forName("driveName");
            System.out.println("驱动启动成功！");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到驱动！");
        }
    }

    /*建立连接*/
    public static Connection getConnection(Connection conn) throws SQLException {
        conn = DriverManager.getConnection(URL, userName, password);
        return conn;
    }

    /*关闭连接*/
    public static void closeConnection(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*释放资源*/
    public static void realeaseAll(ResultSet rs, Statement st, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
